﻿using Cineman.Resources;
using HtmlAgilityPack;
using Microsoft.Phone.Shell;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Cineman.ViewModels
{
    public class MovieModel
    {
        public MovieGroup Today { get; set; }
        public MovieGroup ComingSoon { get; set; }
        public bool IsDataLoaded { get; set; }
        public const string ISKeyToday = "StoredToday";
        public const string ISKeyComingSoon = "StoredComingSoon";
        public bool IsServerReachable { get; set; }
        public static string currLang;

        public async Task<bool> LoadDataAsync()
        {
            GetCurrLang();
            
            Today = await CreateTodayGroupAsync();

            ComingSoon = await CreateComingSoonGroupAsync();            
            IsDataLoaded = true;
            return true;
        }

        public static void GetCurrLang()
        {
            string CurrSystemLang = Thread.CurrentThread.CurrentCulture.Name.Substring(0,2);
            switch (Settings.DisplayLanguage)
            {
                case "0":
                    if (CurrSystemLang == "az")
                    { currLang = ""; }
                    else
                    {
                        if (CurrSystemLang == "ru")
                        { currLang = "ru/"; }
                        else
                        {
                            currLang = "eng/";
                        }
                    }

                    break;
                case "1":
                    currLang = "eng/";
                    break;
                case "2":
                    currLang = "ru/";
                    break;
                case "3":
                    currLang = "";
                    break;
            }
        }

        private async Task<MovieGroup> CreateTodayGroupAsync()
        {

            Uri uri = new Uri("http://www.28cinema.az/" + currLang);
            HttpClient client = new HttpClient();
            HtmlDocument htmlDocument = new HtmlDocument();
            HtmlNode htmlNode = new HtmlNode(0, htmlDocument, 1);
            MovieGroup data = new MovieGroup();
            string HtmlResult;
            try
            {
                HtmlResult = await client.GetStringAsync(uri);
            }
            catch (System.Net.Http.HttpRequestException ex)
            {
                IsServerReachable = false;
                Console.Out.WriteLine(ex.Message);
                HtmlResult = "";
                return null;
            }        
            data.Title = "Today";
            htmlDocument.LoadHtml(HtmlResult);            
            htmlNode = htmlDocument.DocumentNode.SelectSingleNode("//div[@class='poster_wrapper']");

            for (var i = 0; i < (htmlNode.ChildNodes.Count); i++)
            {
                string check = GetAttrFromNode(htmlNode.SelectSingleNode("div[" + (i + 1) + "]/img"), "title");
                if (check != null)
                {
                    data.Items.Add(new MovieData
                    {
                        ID = i,
                        DetailsUrl = GetAttrFromNode(htmlNode.SelectSingleNode("div[" + (i + 1) + "]/div[1]/div[1]/a[1]"), "href"),
                        Title = GetAttrFromNode(htmlNode.SelectSingleNode("div[" + (i + 1) + "]/img"), "title"),
                        Poster = GetAttrFromNode(htmlNode.SelectSingleNode("div[" + (i + 1) + "]/img"), "src"),
                        IsDetailsLoaded = "Collapsed",
                        Cinema = "28 Cinema"
                    });
                }
            }
            return data;
        }

        private async Task<MovieGroup> CreateComingSoonGroupAsync()
        {
            Uri uri = new Uri("http://www.28cinema.az/"+currLang+"soon/");
            HttpClient client = new HttpClient();
            HtmlDocument htmlDocument = new HtmlDocument();
            HtmlNode htmlNode = new HtmlNode(0, htmlDocument, 1);
            MovieGroup data = new MovieGroup();
            string HtmlResult;
            try
            {
                HtmlResult = await client.GetStringAsync(uri);
            }
            catch (System.Net.Http.HttpRequestException ex)
            {
                IsServerReachable = false;
                HtmlResult = "";
                Console.Out.WriteLine(ex.Message);
                return null;
            }   
            data.Title = "Coming soon";
            htmlDocument.LoadHtml(HtmlResult);
            htmlNode = htmlDocument.DocumentNode.SelectSingleNode("//div[@class='poster_wrapper']");
            for (var i = 0; i < (htmlNode.ChildNodes.Count); i++)
            {
                string check = GetAttrFromNode(htmlNode.SelectSingleNode("div[" + (i + 1) + "]/a[1]"), "title");
                if (check != null)
                {
                    data.Items.Add(new MovieData
                    {
                        ID = i,
                        DetailsUrl = GetAttrFromNode(htmlNode.SelectSingleNode("div[" + (i + 1) + "]/a[1]"), "href"),
                        Title = GetAttrFromNode(htmlNode.SelectSingleNode("div[" + (i + 1) + "]/a[1]"), "title"),
                        Poster = GetAttrFromNode(htmlNode.SelectSingleNode("div[" + (i + 1) + "]/a/img"), "src"),
                        IsDetailsLoaded = "Collapsed",
                        Cinema = "28 Cinema"
                    });
                }
            }
            return data;
        }

        private string GetAttrFromNode(HtmlNode p, string attr)
        {
            if (!(p == null))
            {
                return p.GetAttributeValue(attr, "");
            }
            else
            {
                return null;
            };
        }
    }


}
